<template>
  <!-- 定时任务管理 -->
  <div class="box">
    <avue-crud :ref="refName"
               :option="option"
               :page="page"
               :table-loading="tableLoading"
               @row-update="handleUpdate"
               @row-save="handleSave"
               @row-del="rowDel"
               @size-change="sizeChange"
               @current-change="currentChange"
               @refresh-change="refreshChange"
               @search-reset="searchChange"
               @search-change="searchChange"
               v-model="form"
               :data="tableData">
      <template #menu="{ size, row, index }">
        <el-button v-if="buttonPermissionsCheck('JOB_INFO_ADD') && row.status==='PAUSED'"
                   :size="size"
                   @click="run(row.id)"
                   type="text">启动
        </el-button>
        <el-button v-if="buttonPermissionsCheck('JOB_INFO_ADD') && row.status==='RUNNING'"
                   :size="size"
                   @click="pause(row.id)"
                   type="text">暂停
        </el-button>
        <el-button v-if="buttonPermissionsCheck('JOB_INFO_ADD')"
                   :size="size"
                   @click="runOnce(row.id)"
                   type="text">执行
        </el-button>
      </template>
    </avue-crud>

  </div>
</template>

<script setup>

import {option} from '@/table/job/info/list.js'

const httpService = inject('httpService')
const {
  getList,
  form,
  page,
  refName,
  tableData,
  tableLoading,
  handleSave,
  handleUpdate,
  rowDel,
  searchChange,
  refreshChange,
  sizeChange,
  currentChange,
  activated,
  buttonPermissionsCheck,
} = useTableMixins(option, "job-info", {})


/**
 * 运行定时任务
 * @param id 定时任务id
 */
const run = (id) => {
  httpService.put(`/job-info/run/${id}`).then(() => {
    ElMessage.success('定时任务运行成功!')
    getList();
  })
}


/**
 * 运行一次
 * @param id 定时任务id
 */
const runOnce = (id) => {
  httpService.put(`/job-info/runOnce/${id}`).then(() => {
    ElMessage.success('定时任务执行成功!')
  })
}

/**
 * 暂停定时任务
 * @param id 定时任务id
 */
const pause = (id) => {
  httpService.put(`/job-info/pause/${id}`).then(() => {
    ElMessage.success('定时任务暂停成功!')
    getList();
  })
}


onActivated(() => {
  activated()
})
</script>

<style scoped>
.dialog-body {
  height: 500px;
  flex: 1;
  overflow: auto;
}
</style>
